package engine.newUi
{
	import engine.control.loading.MultLoader;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.logic.event.LogicEvent;
	import engine.logic.tips.TipsManage;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.scene.SceneManager;
	import engine.tip.TipsMainLogic;
	import engine.util.FilterUtil;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import newx.StageInfo;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.button.NxButton;

	/**
	 *元旦集字活动逻辑处理 
	 * @author zjing
	 * 
	 */
	public class ExchangeWordLogic
	{
		private static var _self : ExchangeWordLogic;
		private var _ui : ExchangeWordUI;
		private var _model : ModelLocator;
		private var _bindManage:BindManage;
		private var isBind : Boolean;
		
		private var _prizeMCGroup : Array;
		private var _ItemMCGroup : Array;
		private var _needMagicCoin : Array;
		private var _needCount : Array;
		
		private var needExchangeID : int;
		
		public function ExchangeWordLogic()
		{
		}
		public static function getInstance():ExchangeWordLogic
		{
			if(!_self)
			{
				_self = new ExchangeWordLogic();
			}
			return _self;
		}
		
		public function get ui():ExchangeWordUI
		{
			if(!_ui)
			{
				_ui = new ExchangeWordUI();
			}
			return _ui;
		}
		
		public function Init():void
		{
			_model = ModelLocator.getInstance();
			_bindManage = BindManage.getInstance();
		}
		
		public function BeforeShow():void
		{
			this._ui.visible = false;
			if(_ui.uiBG == null)
			{
				MultLoader.getInstance().load([StageInfo.swfResPath+"ExchangeWord.swf"],true,CreateUI)
			}
			else
			{
				addEvent();
			}
		}
		
		public function AfterHide():void
		{
			removeEvent();
		}
		private function addEvent():void
		{
			this._ui.visible = true;
			setGroupUI();
			_ui.ExchangeBtn1.addEventListener(MouseEvent.CLICK,Exchange);
			_ui.ExchangeBtn2.addEventListener(MouseEvent.CLICK,Exchange);
			_ui.ExchangeBtn3.addEventListener(MouseEvent.CLICK,Exchange);
			_ui.closeBtn.addEventListener(MouseEvent.CLICK,closeUI);
			
			for(var i:int;i<_prizeMCGroup.length;i++)
			{
				var prizeMC : MovieClip = _prizeMCGroup[i] as MovieClip;
				prizeMC.addEventListener(MouseEvent.ROLL_OVER,overPrize);
				prizeMC.addEventListener(MouseEvent.ROLL_OUT,outPrize);
			}
			for(var j:int;j<_ItemMCGroup.length;j++)
			{
				var itemMC : MovieClip = _ItemMCGroup[j] as MovieClip;
				itemMC.addEventListener(MouseEvent.ROLL_OVER,overPrize);
				itemMC.addEventListener(MouseEvent.ROLL_OUT,outPrize);
			}
			if(!isBind)
			{
				isBind =true;
				_bindManage.BindView(_model.scene,View.VIEWPORT_TOOL,viewChange,viewChange);
				_bindManage.BindView(_model.scene,View.VIEWPORT_DEPOT,viewChange,viewChange);
			}
			if(!SceneManager._itemIconLoad)
			{
				_model.addEventListener(LogicEvent.ITEMICONLOADED,loaded);
				function loaded():void
				{
					setGroupUI();
				}
			}
		}	
		
		private function removeEvent():void
		{
			_ui.ExchangeBtn1.removeEventListener(MouseEvent.CLICK,Exchange);
			_ui.ExchangeBtn2.removeEventListener(MouseEvent.CLICK,Exchange);
			_ui.ExchangeBtn3.removeEventListener(MouseEvent.CLICK,Exchange);
			_ui.closeBtn.removeEventListener(MouseEvent.CLICK,closeUI);
			for(var i:int;i<_prizeMCGroup.length;i++)
			{
				var prizeMC : MovieClip = _prizeMCGroup[i] as MovieClip;
				prizeMC.removeEventListener(MouseEvent.ROLL_OVER,overPrize);
				prizeMC.removeEventListener(MouseEvent.ROLL_OUT,outPrize);
			}
			for(var j:int;j<_ItemMCGroup.length;j++)
			{
				var itemMC : MovieClip = _ItemMCGroup[j] as MovieClip;
				itemMC.removeEventListener(MouseEvent.ROLL_OVER,overPrize);
				itemMC.removeEventListener(MouseEvent.ROLL_OUT,outPrize);
			}
		}
		
		/**
		 *第一次加载好的UI指针分派 
		 * 
		 */
		private function CreateUI():void
		{
			_ui.windowBG.visible = false;
			_ui.uiBG = MultLoader.getInstance().getSourceByUrl(StageInfo.swfResPath+"ExchangeWord.swf");
			_ui.addChild(_ui.uiBG);
			_ui.setChildIndex(_ui.uiBG,0);
			setGroup();
			addEvent();
		}
		
		private function viewChange(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			setGroupUI();
		}
		
		/**
		 *本地配置文件 
		 * 
		 */
		private function setGroup():void
		{
			_prizeMCGroup = [_ui.uiBG["MC3"],_ui.uiBG["MC8"],_ui.uiBG["MC11"]];
			_ItemMCGroup = [_ui.uiBG["MC1"],
				_ui.uiBG["MC2"],
				_ui.uiBG["MC4"],
				_ui.uiBG["MC5"],
				_ui.uiBG["MC6"],
				_ui.uiBG["MC7"],
				_ui.uiBG["MC9"],
				_ui.uiBG["MC10"]];
			_needMagicCoin = [];
			
			var p:int;
			for(var i:int = 1;i<4;i++)
			{
				var wordArr :　Array = StaticIni.getIniVar("Group_"+i,"Item").split(",");
				for(var j:int=0;j<wordArr.length;j++)
				{
					_ItemMCGroup[p++]["configId"] = wordArr[j];
				}
				var prizeMC : MovieClip = _prizeMCGroup[i-1] as MovieClip;
				prizeMC["configId"] = StaticIni.getIniVar("Group_"+i,"Prize");
				_needMagicCoin.push(int(StaticIni.getIniVar("Group_"+i,"MagicCoin")));
			}
		}
		
		private function setGroupUI():void
		{
			var needCount1 : int;
			var needCount2 : int;
			var needCount3 : int;
			
			for(var i:int;i<_ItemMCGroup.length;i++)
			{
				var itemImg:NxClip = new NxClip();
				var itemMC:MovieClip = _ItemMCGroup[i] as MovieClip;
				var isHaveItem : Boolean = ComposeItemLogic.getInstance().searchItem(itemMC["configId"],1)>0;
				itemImg.source = ResManager.newResClass("icon_"+StaticIni.getIniVar(itemMC["configId"],"Photo")); 
				if(!isHaveItem)
				{
					itemImg.filters = [FilterUtil.GrayColorFilter];
					if(i==0 || i==1)
					{
						needCount1++;
					}
					else if(i==2 || i==3 || i==4 || i==5)
					{
						needCount2++;
					}
					else if(i==6 || i==7)
					{
						needCount3++;
					}
				}
				if(itemMC.numChildren>0)
				{
					itemMC.removeChildAt(0);
				}
				itemMC.addChild(itemImg);
			}
			setBtn();
			_needCount = [needCount1,needCount2,needCount3];
			
			for(var j:int;j < _prizeMCGroup.length; j ++)
			{
				var prizeImg:NxClip = new NxClip();
				var prizeMC:MovieClip = _prizeMCGroup[j] as MovieClip;
				prizeImg.source = ResManager.newResClass("icon_"+StaticIni.getIniVar(prizeMC["configId"],"Photo")); 
				for(var k:int = 0; k < prizeMC.numChildren; k ++)
				{
					prizeMC.removeChildAt(k);
				}
				var effectClip : NxClip = new NxClip();
				effectClip.source = ResManager.newResClass("custSkin_17_5");
				effectClip.x = -16;
				effectClip.y = -16;
				prizeMC.addChild(prizeImg);
				prizeMC.addChild(effectClip);
			}
		}
		
		private function setBtn():void
		{
			if(ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[0]["configId"],1)>0 
				&& ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[1]["configId"],1)>0 )
			{
				ui.ExchangeBtn1.enabled = true;
			}
			else 
			{
				ui.ExchangeBtn1.enabled = false;
			}
			if(ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[2]["configId"],1)>0 
				&& ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[3]["configId"],1)>0
				&& ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[4]["configId"],1)>0 
				&& ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[5]["configId"],1)>0)
			{
				ui.ExchangeBtn2.enabled = true;
			}
			else 
			{
				ui.ExchangeBtn2.enabled = false;
			}
			if(ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[6]["configId"],1)>0 
				&& ComposeItemLogic.getInstance().searchItem(_ItemMCGroup[7]["configId"],1)>0 )
			{
				ui.ExchangeBtn3.enabled = true;
			}
			else 
			{
				ui.ExchangeBtn3.enabled = false;
			}
		}
		
		/**
		 *兑换 
		 * @param e
		 * 
		 */
		private function Exchange(e:MouseEvent):void
		{
			var needMoney : int;
			switch(e.target)
			{
				case _ui.ExchangeBtn1:
					needMoney = _needCount[0]*_needMagicCoin[0];
					needExchangeID = int(_prizeMCGroup[0]["configId"]);
					break;
				case _ui.ExchangeBtn2:
					needMoney = _needCount[1]*_needMagicCoin[1];
					needExchangeID = int(_prizeMCGroup[1]["configId"]);
					break;
				case _ui.ExchangeBtn3:
					needMoney = _needCount[2]*_needMagicCoin[2];
					needExchangeID = int(_prizeMCGroup[2]["configId"]);
					break;
			}
			if(needMoney>0)
			{
				SitAlerLogic.getIntatic().setUIinfo("UIString_1000711",[needMoney],sendExchangeMsg,this);
			}
			else
			{
				sendExchangeMsg();
			}
			
		}
		
		private function sendExchangeMsg():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ITEM_CONVERT,needExchangeID);
			setGroupUI();
		}
		
		private function overPrize(e:MouseEvent):void
		{
			TipsMainLogic.instance.showMain(e.target["configId"]);
		}
		
		private function outPrize(e:MouseEvent):void
		{
			TipsMainLogic.instance.hide();
		}
		
		private function closeUI(e:MouseEvent):void
		{
			_ui.hide();
		}
	}
}